* Create measures of the number of elective and emergency admissions by GP practice in each year - use these in randomisation checks

clear all
set more off

forval year=2005(1)2017{
use "$savedata/ami_`year'", clear
collapse (max) finyear, by(gpprac)
save "$savedata/gp_ids`year'", replace

tab finyear
}


forval year=2005(1)2012{
use "$savedata/gp_ids`year'", clear

merge 1:m gpprac using "$rawdata/ip_`year'"
keep if _merge==3

keep if epiorder==1

gen gp_el = 1 if admimeth>9 & admimeth<20
gen gp_em = 1 if admimeth>19 & admimeth<30

gen gp_el_cardio = 1 if gp_el==1 & tretspef=="320"
gen gp_em_cardio = 1 if gp_em==1 & tretspef=="320"

collapse (sum) gp_el gp_em gp_el_cardio gp_em_cardio, by(gpprac finyear)
su
save "$savedata/gp_ids`year'", replace
}


	forval year=2013(1)2017{

use "$savedata/gp_ids`year'", clear

merge 1:m gpprac using "$rawdata/ip_`year'"
keep if _merge==3

keep if epiorder==1

gen gp_el = 1 if admimeth=="11" | admimeth=="12" | admimeth=="13"
gen gp_em = 1 if substr(admimeth,1,1)=="2"

gen gp_el_cardio = 1 if gp_el==1 & tretspef=="320"
gen gp_em_cardio = 1 if gp_em==1 & tretspef=="320"

collapse (sum) gp_el gp_em gp_el_cardio gp_em_cardio, by(gpprac finyear)
su
save "$savedata/gp_ids`year'", replace

}



use "$savedata/gp_ids2005", clear

forval year=2006(1)2017{
	append using "$savedata/gp_ids`year'"
}

save "$savedata/gp_pastadmits0517.dta", replace
